package dev.bong.service.impl;

import dev.bong.dto.ApplyDTO;
import dev.bong.mapper.LabApplyMapper;
import dev.bong.mapper.UserMapper;
import dev.bong.pojo.entity.LabApply;
import dev.bong.service.StudentService;
import dev.bong.utils.ThreadLocalUtil;
import dev.bong.vo.ApplyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/4/15 16:46
 * @version: 1.0
 */

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    private LabApplyMapper labApplyMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer getStatusById(Integer id) {

        Integer status=labApplyMapper.getApplyById(id).getApplyStatus();
        return status;
    }

    @Override
    public List<ApplyVo> getApplyList() {

        Map<String,Object> map=ThreadLocalUtil.get();

        Integer studentId=(Integer) map.get("userId");
        String semester=(String) map.get("semester");

        System.out.println("studentId:"+studentId+" semester:"+semester);

        List<LabApply> list=labApplyMapper.getApplyBySemesterAndStudentId(semester, studentId);
        System.out.println(list.toString());
        List<ApplyVo> ans=new ArrayList<>();
        String studentName=userMapper.getNameById(studentId);
        for(LabApply labApply:list){
            ApplyVo vo= ApplyVo.builder()
                    .id(labApply.getId())
                    .semester(labApply.getSemester())
                    .weeks(labApply.getWeeks())
                    .section(labApply.getSection())
                    .labNumber(labApply.getLabNumber())
                    .applyReason(labApply.getApplyReason())
                    .applyStatus(labApply.getApplyStatus())
                    .applyTime(labApply.getApplyTime())
                    .studentName(studentName)
                    .build();
            ans.add(vo);
        }
        return ans;

    }

    @Override
    public void addApply(ApplyDTO dto) {

        Map<String,Object> map= ThreadLocalUtil.get();
        Integer studentId=(Integer) map.get("userId");
        String semester = (String) map.get("semester");

        LabApply labApply =LabApply.builder()
                .semester(semester)
                .weeks(dto.getWeeks())
                .section(dto.getSection())
                .labNumber(dto.getLabNumber())
                .applyReason(dto.getApplyReason())
                .applyTime(dto.getApplyTime())
                .applyStatus(-1)
                .studentId(studentId)
                .build();

        labApplyMapper.addApply(labApply);

    }

    @Override
    public void updateApply(ApplyDTO dto, Integer applyId) {

        LabApply labApply = LabApply.builder()
                .id(applyId)
                .weeks(dto.getWeeks())
                .section(dto.getSection())
                .labNumber(dto.getLabNumber())
                .applyReason(dto.getApplyReason())
                .applyTime(dto.getApplyTime())
                .build();

        labApplyMapper.updateApply(labApply);
    }

    @Override
    public void confirmApply(Integer applyId) {

        labApplyMapper.confirmApply(applyId);
    }
}
